package cn.jack.mvvm

import cn.jack.lib_common.CustomApplication
import cn.jack.lib_common.flutter.base.FlutterRootActivity
import cn.jack.lib_common.flutter.bridge.FlutterBridge
import cn.jack.lib_common.flutter.cache.FlutterCacheManager
import cn.jack.library_util.ActivityManager
import cn.jack.library_util.ToastU
import com.alibaba.android.arouter.launcher.ARouter
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostDelegate
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import dagger.hilt.android.HiltAndroidApp
import io.flutter.embedding.android.FlutterActivityLaunchConfigs


/**
 * @创建者 Jack
 * @创建时间 2022/8/29 0029 12:29
 * @描述
 */
@HiltAndroidApp
class AppApplication : CustomApplication() {

    override fun initArouter() {
        if (isDebugMode()) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }

    override fun initOthers() {
        super.initOthers()
        initActivityManager()

        //更改实现方案 ---> initFlutterBoost    FlutterBoost目前不支持Android引擎预热
//        FlutterCacheManager.instance?.preLoad(this, MultipleModulesNameProvider())
        //在 Android 平台上创建自定义 FlutterEngine  给FlutterBoost提供默认的FlutterEngin
//        FlutterCacheManager.instance?.preLoad(this,FlutterCacheManager.customEnginName)//需要在执行位置做销毁操作
//        FlutterCacheManager.instance?.preLoad(
//            this,
////            FlutterBoost.ENGINE_ID,
//            FlutterCacheManager.customEnginName,
//            object : FlutterCacheManager.InitCallback {
//                override fun initFinish(engine: FlutterEngine?) {
//                    initFlutterBoost()
//                }
//            })

//        initFlutterBoost()
        //优化：预加载dartVM，效果比较明显
        FlutterCacheManager.instance?.preLoadDartVM(this) {
            initFlutterBoost()
        }
    }

//    class DefaultFlutterEngineProvider : FlutterEngineProvider {
//        override fun provideFlutterEngine(context: Context): FlutterEngine? {
//            return FlutterCacheManager.instance?.getCachedFlutterEngine(
//                ContextU.context(),
//                FlutterCacheManager.customEnginName
//            )
//        }
//    }

    private fun initFlutterBoost() {

        // 初始化 Flutter Boost
        // Cache the created FlutterEngine in the FlutterEngineCache.
//        FlutterEngineCache.getInstance().put(FlutterBoost.ENGINE_ID, flutterEngine)
//        var builder = FlutterBoostSetupOptions.Builder()
//        builder
//            .initialRoute("/")
//            .dartEntrypoint("main")
//            .flutterEngineProvider(DefaultFlutterEngineProvider())

        FlutterBoost.instance().setup(this, object : FlutterBoostDelegate {
            override fun pushNativeRoute(options: FlutterBoostRouteOptions) {
                //这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个
//                val intent = Intent(
//                    FlutterBoost.instance().currentActivity(),
//                    YourTargetAcitvity::class.java
//                )
//                FlutterBoost.instance().currentActivity()
//                    .startActivityForResult(intent, options.requestCode())
            }

            override fun pushFlutterRoute(options: FlutterBoostRouteOptions) {
                val intent = FlutterBoostActivity.CachedEngineIntentBuilder(
                    FlutterRootActivity::class.java//首次打开flutter页面，状态栏未生效
//                            FlutterBoostActivity::class.java
                )
                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.opaque)//若设置为transparent会存在黑屏
                    .destroyEngineWithActivity(false).uniqueId(options.uniqueId())
                    .url(options.pageName()).urlParams(options.arguments())
                    .build(FlutterBoost.instance().currentActivity())
                FlutterBoost.instance().currentActivity().startActivity(intent)
            }
        }) {
            //初始化flutter与native通信的插件
            FlutterBridge.init(it)
        }
//        }, builder.build())
    }

    private fun initActivityManager() {
        ActivityManager.instance.init(this)
        ActivityManager.instance.addFrontBackCallback(object : ActivityManager.FrontBackCallback {
            override fun onChanged(front: Boolean) {
                ToastU.normal("当前处于 $front")
            }
        })
    }

}